import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import router from './router'
import store from './store'
import panelHead from './components/panelHead.vue'

// 刷新后的动态路由添加
const localData = localStorage.getItem('pz_v3pz');
if (localData) {
	store.commit('dynamicMenu', JSON.parse(localData).menu.routerList);
	store.state.menu.routerList.forEach(item => {
		router.addRoute('main', item);
	});
}

router.beforeEach((to, from) => {
	const token = localStorage.getItem('pz_token');
	// 非登录界面token不存在
	if (!token && to.path !== '/login') {
		return '/login';
	}
	// 已经登录且还在访问login界面，此时则跳回根目录
	// 这种情况很难考虑到
	else if (token && to.path === '/login') {
		return '/';
	}
	else {
		return true;
	}

});

import * as ElementPlusIconsVue from '@element-plus/icons-vue'

const app = createApp(App)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
	// 全局注册
	app.component(key, component)
}

app.component('panelHead', panelHead)
app.use(router)
// store挂载
app.use(store)
app.mount('#app')